package com.kingdee.hackson.controller;

import com.kingdee.hackson.service.IPlatformSchemeService;
import com.kingdee.hackson.service.impl.InitOrgIdServiceImpl;
import com.kingdee.hackson.service.impl.PlatformSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/platformScheme")
public class PlatformSchemeController {

    @Autowired
    IPlatformSchemeService service;

    // 直接通过in的方式查询 异步分页 http://localhost:8080/platformScheme/in/1/20
    @GetMapping("/in/{pageNum}/{count}")
    public Map<String, Object> getInSchemeResult(@PathVariable int pageNum, @PathVariable int count) {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            service.executeInScheme(pageNum, count, responseMap);
            responseMap.put("resultCode", 200);
        } catch (Exception ex) {
            ex.printStackTrace();
            responseMap.put("resultCode", 500);
        }
        return responseMap;
    }

    // 直接通过in的方式查询 count http://localhost:8080/platformScheme/in/count
    @GetMapping("/in/count")
    public Map<String, Object> getInSchemeResultCount() {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            long count = service.getInSchemeResultCount(responseMap);
            responseMap.put("count", count);
            responseMap.put("resultCode", 200);
        } catch (Exception ex) {
            ex.printStackTrace();
            responseMap.put("resultCode", 500);
        }
        return responseMap;
    }

    // 多线程切割in条件查询 异步分页 http://localhost:8080/platformScheme/multiIn/1/20
    @GetMapping("/multiIn/{pageNum}/{count}")
    public Map<String, Object> getMultiInSchemeResult(@PathVariable int pageNum, @PathVariable int count) {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            service.executeInByMultiThread(pageNum, count, responseMap);
            responseMap.put("resultCode", 200);
        } catch (Exception ex) {
            ex.printStackTrace();
            responseMap.put("resultCode", 500);
        }
        return responseMap;
    }

    // 临时表方案 异步分页 http://localhost:8080/platformScheme/query/1/20
    @GetMapping("/query/{pageNum}/{count}")
    public Map<String, Object> getPlatformSchemeResult(@PathVariable int pageNum, @PathVariable int count) {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            service.executePlatformScheme(pageNum, count, responseMap);
            responseMap.put("resultCode", 200);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 删除临时表
            service.deleteTmpTableForException();
            responseMap.put("resultCode", 500);
        }
        return responseMap;
    }

    // 临时表方案-多线程插入 异步分页 http://localhost:8080/platformScheme/query/threads/1/20
    @GetMapping("/query/threads/{pageNum}/{count}")
    public Map<String, Object> getPlatformSchemeResultByMultiThreads(@PathVariable int pageNum, @PathVariable int count) {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            service.executePlatformSchemeByMultiThread(pageNum, count, responseMap);
            responseMap.put("resultCode", 200);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 删除临时表
            service.deleteTmpTableForException();
            responseMap.put("resultCode", 500);
        }
        return responseMap;
    }

    // 临时表方案 count http://localhost:8080/platformScheme/query/count
    @GetMapping("/query/count")
    public Map<String, Object> getPlatformSchemeCount() {
        Map<String, Object> responseMap = new HashMap<>();
        try {
            long count = service.getPlatformSchemeCount(responseMap);
            responseMap.put("count", count);
            responseMap.put("resultCode", 200);
        } catch (Exception ex) {
            ex.printStackTrace();
            // 删除临时表
            service.deleteTmpTableForException();
            responseMap.put("resultCode", 500);
        }
        return responseMap;
    }
}
